#!/usr/bin/python
#coding=utf8
"""
# Author: Kellan Fan
# Created Time : Wed 20 Sep 2023 06:17:50 PM CST

# File Name: heale_event.py
# Description:

"""
import redis
import yaml
import datetime
import json

def cur_date():
    now = datetime.datetime.now()
    return now.strftime("%Y-%m-%d")

def connect_redis():
    with open("/root/config/redis.yaml", mode = 'r', encoding='utf-8') as f:
        redis_info = yaml.safe_load(f)
    try:
        redis_pool = redis.ConnectionPool(host=redis_info['host'],port=redis_info['port'],password=redis_info['password'])
        return redis.Redis(connection_pool=redis_pool)
    except:
        print('Connect to redis failed')
        return none

def handle_event():
    st = cur_date()
    redis_conn = connect_redis()
    for value in redis_conn.sort('event-'+st, alpha=True, desc=True):
        alerts = json.loads(value)['alerts']
        for alert in alerts:
            if alert['status'] == 'firing':
                try:
                    print(alert['annotations']['description'])
                except:
                    print(alert)
                
            

if __name__ == '__main__':
    handle_event()
